Skip to main content

Reading File Contents

There are many ways to read the contents of files even with restrictions imposed.

Table of Contents
  • Basic Commands - Linux
  • Basic Commands - Windows
  • Linux Shell Functions

Basic Commands - Linux

We can use commands such as cat and less on Linux to print the contents of the file to our terminal.

Using cat and less:

cat File_Name_Here
less File_Name_Here

Example:

cat myfile.txt
less myfile2.txt

Basic Commands - Windows

In Windows, we can use type and more on Windows to print the contents of the file to our terminal.

Using type and more:

type File_Name_Here
more File_Name_Here

Example:

type myfile3.txt
more myfile4.txt

Linux Shell Functions

If we do not have any binaries or are locked to certain ones, we can use built in shell functions to read the file.

The following commands will use the shell read function where the contents of a file called top-secret.txt is saved into a variable called OUTPUT.

read OUTPUT < /secret/top-secret.txt

Command breakdown:

  • read - Use the in-built read function.
  • OUTPUT - Specify the variable to save the contents to.
  • < /secret/top-secret.txt - Specify to save the contents of the file into the OUTPUT variable.

Once done, we can use the following command to show the contents.

read $OUTPUT

Command breakdown:

  • $OUTPUT - Specify the variable to read to show its contents.